<template>
  <div class="dialog-form-wrapper">
    <el-dialog
      class="dialog-wrapper"
      :title="empty === '0' ? '新增' : '修改'"
      :visible="visible"
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-form
        ref="dialogForm"
        class="dialog-form-content"
        :rules="rules"
        :model="form"
        :label-width="labelWidth || '6em'"
      >
        <el-form-item
          v-for="(item, index) in dialogForm"
          :key="index"
          :label="item.label + ':'"
          :prop="item.prop"
        >
          <template>
            <el-date-picker
              v-if="item.type === 'datetime'"
              v-model="form[item.field]"
              type="datetime"
              suffix-icon="el-icon-date"
              value-format="yyyy-MM-dd HH:mm"
              :placeholder="'请选择' + item.label"
            />
            <el-select
              v-else-if="item.type === 'select'"
              v-model="form[item.prop]"
              :placeholder="'请选择' + item.label"
            >
              <el-option
                v-for="(op, idx) in item.options"
                :key="op.value + idx"
                :label="op.label"
                :value="op.value"
              />
            </el-select>
            <el-input
              v-else
              v-model="form[item.prop]"
              :type="item.type"
              :placeholder="'请输入' + item.label"
              autosize
            />
          </template>
        </el-form-item>
      </el-form>

      <el-footer class="dialog-footer">
        <el-button type="info" @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'DialogForm',
  props: {
    empty: { type: String },
    visible: { type: Boolean },
    form: { type: Object },
    dialogForm: { type: Array },
    rules: { type: Object },
    labelWidth: { type: String },
  },
  methods: {
    handleClose() {
      this.$emit('handle-cancel');
      this.$refs.dialogForm.resetFields();
    },
    handleSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        valid && this.$emit('handle-submit', valid);
        return valid;
      })
    }
  }
}
</script>

<style lang="scss">
.dialog-form-wrapper {
  .dialog-footer {
    text-align: center;
    height: auto !important;
  }
}
</style>
